<?php
$this->useClass("Action_Community_FeedHandler");
class Action_Community_ShowFiles extends Action_Community_FeedHandler
{
	var $ajax_functions = array(
		'ajax_getSomePosts',
		);

	public $types;

	public function getFeedModel() {
		$this->rh->UseClass("AggregatedModel_Feed_FeedInterface");
		$this->rh->UseClass("AggregatedModel_Feed_ReaderFiles");

		return new AggregatedModel_Feed_ReaderFiles();
	}

	public function ajax_getSomePosts( $page_number ) {
		$this->setParam("page_number", $page_number);
		$this->model->doStuff();

		$view = $this->rh->views->findByName('items:files');
		$view->data = $this->acl_assistant->template_data;
		$view->data['files_list'] = $this->getPreparedItems();

		$items_node = &$this->rh->ctx->cp->add_node("items");
		$items_node->set_data( $view->getContent() ); // рендеринг итемов

		$this->addPagerNodes( &$this->rh->ctx->cp );

		$this->shutdown();
	}

	protected function init() {
		parent::init();
		$this->m_tdi = $this->ctx->mm->getModel("Tdi", "m");
		$tdi_oids = array();
		foreach ($this->aa->acl_data['tdi_list'] as $tdi) $tdi_oids[] = $tdi['id_object'];
		$this->tdi_types = $this->m_tdi->getSections($tdi_oids); // структура для вывода в шаблоны
	}


	public function getWidgetsConfig ()
	{
		$widgets = array();

		$widgets[] = array (
			'name' => 'CreateBtnWidget',
			'params' => array(
				'widget_position' => 'createbtn',
				'widget_view' => 'WG_CreateBtnWidget',
				'use_doctypes' => array('file'),
			),
		);

		$widgets[] = array (
			'name' => 'Feed_DoctypesMenuWidget',
			'params' => array(
				'widget_position' => 'tags',
				'widget_view' => 'WG_Feed_DoctypesMenuWidget',
			),
		);

		$widgets[] = array (
			'name' => 'TagsWidget',
			'params' => array(
				'community_oid' => $this->aa->acl_data['comity_oid'],
				'tdi_oid'=> $this->aa->acl_data['tdi_oid'],
				'tag' => $this->getParam('tag'),
				'widget_position' => 'tags',
				'widget_view' => 'WG_TagsWidget',
				'view_type' => 'files',
			),
		);

		$widgets[] = array (
			'name' => 'RssWidget',
			'params' => array(
				'widget_view' => 'WG_RssWidget',
				'widget_position' => 'tags',
			),
		);

		return $widgets;
	} // end of function getWidgetsConfig

	protected function shutdown() {
		$this->ctx->db->end();
	}

	protected function determineCustomModelParams() {

		// --- file types --- {{{
		$types = array_keys( $this->rh->GET['ftypes'] );
		// если установлена галка "Все", то все остальное херим
		if ( $types[0] === 0 ) unset($types); else {
			$this->setParam("types", $types);
		}
		// --- file types --- }}}
	}

	private function getPreparedItems() {
		$items = $this->model->getItems();
		foreach ($items as $k=>$item) {

			// сгенерировать URL для странички с комментарием
			$ug = new RL_URLGenerator($this->rh->router->getRule('showitem'));

			$ug->setRouter($this->rh->router);

			$params = $this->params; $params['id_item'] = $item['item_oid'];
			$ug->setParams($params);

			$items[$k]['item_url'] = $ug->generateUrl();

		}
		return $items;
	}

	public function process ()
	{
		$tag = $this->tag;
		$types = $this->types;
		$order_by = $this->order_by;
		$page_number = $this->page_number;

		$aa = &$this->aa;

		$aa->checkDeletions(array($this, "shutdown") );

		$this->model->doStuff();

		$view = $this->rh->views->findByName('files');
		$view->data = $aa->template_data;

		$view->data['can_create'] = $aa->is_granted(array('item_create', 'item_create_premod'));

		$view->data['files_list'] = $this->getPreparedItems();
		$pager = $this->getPagerInfo();
		$view->data['page'] = $pager;
		$view->data['items_remaining'] = $pager['items_remaining'];

		$view->data['files_count'] = $this->model->getFilesCount();
		$view->data['types_list'] = $this->model->getFileTypes('types');

		$view->data['_sort'] = $this->getParam('order_by');

		$this->renderFilters($view);

		$view->data['__widgets__'] = $this->widgets;

		// для постраничной листалки ajax handler
		$this->rh->UseClass("RL_URLGenerator");

		$ug = new RL_URLGenerator($this->rh->router->getLastMatched()); //$this->rh->router->getRule('object_undelete')
		$ug->setRouter($this->rh->router);
		$ug->setParams($this->params);

		// урл для постраничной листалки без ajax
		if (isset($pager['prev'])) {
			$ug->setGet(array_merge($this->rh->GET, array("p"=>$pager['prev'])));
			$view->data["prev_url"] = $ug->generateUrl();
		}

		if (isset($pager['next'])) {
			$ug->setGet(array_merge($this->rh->GET, array("p"=>$pager['next'])));
			$view->data["next_url"] = $ug->generateUrl();
		}

		unset($ug);
		if ( isset($this->params['tdi_url']) ) {
			$ug = new RL_URLGenerator($this->rh->router->getRule('showfiles_tdi_ajax'));
		} else {
			$ug = new RL_URLGenerator($this->rh->router->getRule('showfiles_ajax'));
		}
		$ug->setRouter($this->rh->router);
		$ug->setParams($this->params);

		$ug->setGet($this->rh->GET);

		$view->data['ajax_url'] = $ug->generateUrl();

		$this->rh->response->setContent( $view );

		// -- touch visitor stat -- {{{
		// visit  tdi
		$this->ctx->mm->getModel("Visitable", "m")->getVisitable('Tdi')->visit();
		//
		// -- touch visitor stat -- }}}

	} // end of function execute
}
